package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.SysModuleTreeAddRequest;
import com.mes.cloud.api.dto.request.SysModuleTreeRequest;
import com.mes.cloud.api.dto.request.SysModuleTreeUpdateRequest;
import com.mes.cloud.api.dto.response.ParentTreeDto;
import com.mes.cloud.api.dto.response.SysModuleTreeDto;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysModuleTreeDao;
import com.mes.cloud.entity.SysModuleTree;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 模块树表
 *
 * @author Mr.zhang
 * @version 2019-07-30 13:43:20
 * @email 407892370@qq.com
 */
@Service
public class SysModuleTreeService {

    private SysModuleTreeDao sysModuleTreeDao;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public SysModuleTreeService(SysModuleTreeDao sysModuleTreeDao, IPKeyGenerator ipKeyGenerator) {
        this.sysModuleTreeDao = sysModuleTreeDao;
        this.iPKeyGenerator = ipKeyGenerator;
    }

    /**
     * 描述 模块树列表
     *
     * @param {sysModuleTreeRequest]
     * @return java.util.List<com.shinho.crm.dict.api.dto.response.SysModuleTreeDto>
     * @author 郑样平
     * @date 2019/8/8 17:49
     */
    public List<SysModuleTreeDto> list(SysModuleTreeRequest sysModuleTreeRequest) {
        //查询条件
        SysModuleTree selectParam = new SysModuleTree();
        sysModuleTreeRequest.setDelFlag(Constants.DEL_FLAG_NO);
        BeanUtils.copyProperties(sysModuleTreeRequest, selectParam);
        List<SysModuleTree> sysModuleTreeList = sysModuleTreeDao.sysModuleTreeList(selectParam);
        List<SysModuleTreeDto> sysModuleTreeDtoList = new ArrayList<>();
        for (SysModuleTree sysModuleTree : sysModuleTreeList) {
            SysModuleTreeDto sysModuleTreeDto = new SysModuleTreeDto();
            BeanUtils.copyProperties(sysModuleTree, sysModuleTreeDto);
            sysModuleTreeDtoList.add(sysModuleTreeDto);
        }
        //排序
        //Collections.sort(sysModuleTreeDtoList, Comparator.comparing(SysModuleTreeDto::getName));
        //sysModuleTreeDtoList.sort((a, b) -> (a.getCreateTime()).compareTo(b.getCreateTime()));
        return sysModuleTreeDtoList;
    }

    /**
     * 描述 新增模块树
     *
     * @param {sysModuleTreeRequest]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/8 17:50
     */
    @Transactional
    public JsonResult<SysModuleTreeDto> insertSelective(SysModuleTreeAddRequest sysModuleTreeAddRequest) {
        JsonResult jsonResult = new JsonResult();
        //判断编码是否已经存在
        if (!StringUtils.isEmpty(sysModuleTreeAddRequest.getCode())) {
            SysModuleTree sysModuleTree1 = new SysModuleTree();
            sysModuleTree1.setCode(sysModuleTreeAddRequest.getCode());
            sysModuleTree1.setDelFlag(Constants.DEL_FLAG_NO);
            sysModuleTree1.setType(sysModuleTreeAddRequest.getType());
            int moduleCount = sysModuleTreeDao.selectCount(sysModuleTree1);
            if (moduleCount > 0) {
                jsonResult.setMsg(HttpStatusEnum.TREE_CODE.getMessage());
                jsonResult.setCode(HttpStatusEnum.TREE_CODE.getCode());
                return jsonResult;
            }
        }
        SysModuleTree sysModuleTree = new SysModuleTree();
        BeanUtils.copyProperties(sysModuleTreeAddRequest, sysModuleTree);
        sysModuleTree.setId(iPKeyGenerator.getGenerateKey());
        sysModuleTree.setUpdateDateStr(DateUtil.updateDateStr());
        sysModuleTree.setCreateBy(UserUtil.getCreateByName());
        sysModuleTree.setUpdateBy(UserUtil.getUpdateByName());
        sysModuleTree.setCreateByNo(UserUtil.getCreateByNo());
        sysModuleTree.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysModuleTreeDao.insertSelective(sysModuleTree);
        if (count > 0) {
            SysModuleTree sysModuleTree1 = sysModuleTreeDao.selectByPrimaryKey(sysModuleTree.getId());
            SysModuleTreeDto sysModuleTreeDto = new SysModuleTreeDto();
            if (sysModuleTree1 != null) {
                BeanUtils.copyProperties(sysModuleTree1, sysModuleTreeDto);
            }
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
            jsonResult.setData(sysModuleTreeDto);
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        }
        return jsonResult;
    }

    /**
     * 描述 修改模块树
     *
     * @param {sysModuleTreeRequest]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/8 17:51
     */
    public JsonResult updateSelectiveById(SysModuleTreeUpdateRequest sysModuleTreeUpdateRequest) {
        JsonResult jsonResult = new JsonResult();
        //判断编码是否已经存在
        if (!StringUtils.isEmpty(sysModuleTreeUpdateRequest.getCode())) {
            SysModuleTree sysModuleTree = sysModuleTreeDao.selectByPrimaryKey(sysModuleTreeUpdateRequest.getId());
            SysModuleTree sysModuleTree1 = new SysModuleTree();
            sysModuleTree1.setCode(sysModuleTreeUpdateRequest.getCode());
            int moduleCount = sysModuleTreeDao.selectCount(sysModuleTree1);
            if (!sysModuleTree.getCode().equals(sysModuleTreeUpdateRequest.getCode()) && moduleCount > 0) {
                jsonResult.setMsg(HttpStatusEnum.TREE_CODE.getMessage());
                jsonResult.setCode(HttpStatusEnum.TREE_CODE.getCode());
                return jsonResult;
            }
        }
        SysModuleTree sysModuleTree = new SysModuleTree();
        BeanUtils.copyProperties(sysModuleTreeUpdateRequest, sysModuleTree);
        sysModuleTree.setUpdateDateStr(DateUtil.updateDateStr());
        sysModuleTree.setUpdateBy(UserUtil.getUpdateByName());
        sysModuleTree.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysModuleTreeDao.updateByPrimaryKeySelective(sysModuleTree);
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        }
        return jsonResult;
    }

    /**
     * 描述 模块树删除
     *
     * @param {id]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/8 17:52
     */
    public JsonResult deleteById(String id) {
        JsonResult jsonResult = new JsonResult();
        SysModuleTree selectParam = new SysModuleTree();
        selectParam.setParentId(id);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        int count1 = sysModuleTreeDao.selectCount(selectParam);
        if (count1 > 0) {
            jsonResult.setMsg(HttpStatusEnum.TREE_LEAF_FLAG_YES.getMessage());
            jsonResult.setCode(HttpStatusEnum.TREE_LEAF_FLAG_YES.getCode());
            return jsonResult;
        }
        SysModuleTree selectParam1 = new SysModuleTree();
        selectParam1.setId(id);
        selectParam1.setDelFlag(Constants.DEL_FLAG_YES);
        selectParam1.setUpdateDateStr(DateUtil.updateDateStr());
        selectParam1.setUpdateBy(UserUtil.getUpdateByName());
        selectParam1.setUpdateByNo(UserUtil.getUpdateByNo());
        Integer count2 = sysModuleTreeDao.updateByPrimaryKeySelective(selectParam1);
        if (count2 == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        }
        return jsonResult;
    }

    /**
     * 描述 获取模块树详情
     *
     * @param {id]
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.dict.api.dto.response.SysModuleTreeDto>
     * @author 郑样平
     * @date 2019/8/8 17:53
     */
    public JsonResult<SysModuleTreeDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        SysModuleTree sysModuleTree = sysModuleTreeDao.selectByPrimaryKey(id);
        if (sysModuleTree == null) {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
            return jsonResult;
        }
        SysModuleTreeDto sysModuleTreeDto = new SysModuleTreeDto();
        BeanUtils.copyProperties(sysModuleTree, sysModuleTreeDto);
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(sysModuleTreeDto);
        return jsonResult;
    }

    /**
     * 描述 根据ID递归拼接name
     *
     * @param id
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.dict.api.dto.response.ParentTreeDto>
     * @author 郑样平
     * @date 2019/10/10 10:24
     */
    public JsonResult<ParentTreeDto> parentTree(String id) {
        ParentTreeDto dto = new ParentTreeDto();
        dto.setParentTree(getFamilyName(id));
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(dto);
    }

    private String getFamilyName(String id) {
        String familyName = "";
        if (!id.equals(Constants.PAREN_TTREE)) {
            SysModuleTree sysModuleTree = sysModuleTreeDao.selectByPrimaryKey(id);
            if (sysModuleTree != null) {
                familyName = sysModuleTree.getName();
                familyName = getFamilyName(sysModuleTree.getParentId()) + "/" + familyName;
            }
        }
        return familyName;
    }

}
